
MEM_CHUNK_INC_STEP	equ 4*1024

_@mem_chunk	struc
	current	dd ?
	begin	dd ?
	total	dd ?
_@mem_chunk		ends

.data?

.data


.code
;---------------------------------------------------------------------------------
NewMemChunk	proc
			push edi
			push esi
		_@getMem sizeof _@mem_chunk
			test eax,eax
			jz short nmcExit

			mov edi,eax
		assume edi: ptr _@mem_chunk

			mov esi,1024*MEM_CHUNK_INC_STEP
		_@getMem esi
			test eax,eax
			jnz short @f

		_@freeMem edi
			xor eax,eax
			jmp short nmcExit
		@@:

			mov [edi].begin,eax
			mov [edi].current,eax
			mov [edi].total,esi

			mov eax,edi
	nmcExit:
			pop esi
			pop edi
			ret
NewMemChunk	endp
;---------------------------------------------------------------------------------
CheckMemChunkSize	proc lpMemChunk, _size_
			push esi
			push edi
			push ecx

			mov eax,lpMemChunk
			mov esi,eax
			push eax
			call ____testPointer
			test eax,eax
			jz short cmcsExit
		assume esi: ptr _@mem_chunk
;jmp cmcsOK

			mov edi,[esi].current
			sub edi,[esi].begin
			mov eax,edi
			add eax,_size_

			mov ecx,[esi].total
			cmp ecx,eax
			jnc short cmcsOK

		mem_size_find_loop_b:
			cmp ecx,eax
			jnc short mem_size_find_loop_e

			add ecx,MEM_CHUNK_INC_STEP
			jmp short mem_size_find_loop_b
		mem_size_find_loop_e:

			mov [esi].total,ecx

			push ecx
			push [esi].begin
			call ___@regetMem

			mov [esi].begin,eax
			add eax,edi
			mov [esi].current,eax

	cmcsOK:
			mov eax,[esi].current
	cmcsExit:
			pop ecx
			pop edi
			pop esi
			ret
CheckMemChunkSize	endp
;---------------------------------------------------------------------------------
StoreStringInModBinChunk	proc lpMemChunk,lpszStr
			push esi
			push edi

			mov esi,lpMemChunk
		assume esi: ptr _@mem_chunk

			mov edi,[esi].current

			mov eax,lpszStr
			test eax,eax
			jz short @f

			push eax
			call lstrlen

			test eax,eax
			jz short @f

			inc eax
		@@:
			push eax

			add eax,4
			push eax
			push esi
			call CheckMemChunkSize
			mov edi,eax

			pop ecx

;			push ecx
		_@mod_interf_cr_compute_len ecx
		_@stos_len ecx,eax
;			pop ecx

			test ecx,ecx
			jz short @f

			mov eax,lpszStr

;			test eax,eax
;			jz short @f

			push ecx		;len
			push eax		;from
			push edi		;to
		add edi,ecx
		mov [esi].current,edi
			call ___@cpyMem;lstrcpyn ;
		@@:

			mov eax,edi
	ssimbcExit:

			pop edi
			pop esi
			ret
StoreStringInModBinChunk	endp
;---------------------------------------------------------------------------------
CrMainModBinChunk	proc lpMemChunk,lpszName,lpszComment,lpReadFunc,lpWriteFunc,lpCallFunc
			push edi
			push esi
			push ecx

			mov eax,lpMemChunk
			mov esi,eax
			push eax
			call ____testPointer
			test eax,eax
			jz  cmmcExit;short
		assume esi: ptr _@mem_chunk

	; begining preparation
;			mov eax,[esi].begin
;;			mov edi,eax
;			mov [esi].current,eax

;			push 4
;			push esi
;			call CheckMemChunkSize
;			mov edi,eax
;
;		_@mod_interf_cr_chunk_begin edi

	;store name
			push lpszName
			push esi
			call StoreStringInModBinChunk

	;store comment
			push lpszComment
			push esi
			call StoreStringInModBinChunk
			mov [esi].current,eax

			push 4*3
			push esi
			call CheckMemChunkSize
			mov edi,eax

			mov ecx,lpReadFunc
		_@mod_interf_cr_compute_len ecx
		_@stos_len ecx,eax

			mov ecx,lpWriteFunc
		_@mod_interf_cr_compute_len ecx
		_@stos_len ecx,eax

			mov ecx,lpCallFunc
		_@mod_interf_cr_compute_len ecx
		_@stos_len ecx,eax

			mov [esi].current,edi


;			push 4*3
;			push esi
;			call CheckMemChunkSize
;			mov edi,eax
;
;			push edi
;			call _@mod_interf_cr_chunk_end 
;			mov edi,eax

	cmmcExit:
			pop ecx
			pop esi
			pop edi
			ret
CrMainModBinChunk	endp
;---------------------------------------------------------------------------------
CrModMembBinChunk	proc lpMemChunk,lpszName,lpszComment,membType,lpMemb
			push edi
			push esi
			push ecx

			mov eax,lpMemChunk
			mov esi,eax
			push eax
			call ____testPointer
			test eax,eax
			jz short cmmcExit
		assume esi: ptr _@mem_chunk

	; begining preparation
		_@mod_interf_cr_chunk_begin [esi].current

	;store name
			push lpszName
			push esi
			call StoreStringInModBinChunk

	;store comment
			push lpszComment
			push esi
			call StoreStringInModBinChunk
			mov [esi].current,eax

			push 4*2
			push esi
			call CheckMemChunkSize
			mov edi,eax

			mov eax,membType
			stosb

			test al,al
			jnz short @f

			stosb
		@@:

			mov eax,lpMemb
			stosd

			mov [esi].current,edi

			push 4
			push esi
			call CheckMemChunkSize
			mov edi,eax

			push edi
			call _@mod_interf_cr_chunk_end 
			mov edi,eax
			mov [esi].current,edi

	cmmcExit:
			pop ecx
			pop esi
			pop edi
			ret
CrModMembBinChunk	endp
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------
;---------------------------------------------------------------------------------


